package com.twl.ws.web;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.twl.app.base.ctrl.CmsServerCtrlImpl;
import com.twl.app.base.ctrl.CommonCtrlImpl;
import com.twl.app.base.ctrl.DutyseviceImp;
import com.twl.app.base.entity.OpenState;
import com.twl.app.base.entity.Uemp;
import com.twl.app.base.util.GsonUtils;
import com.twl.app.base.util.WsPageUtils;
import com.twl.app.base.vo.Moneydetail;
import com.twl.app.base.vo.Pagevo;
import com.twl.core.framework.page.Result;
import com.twl.facade.web.CustomerFacade;

@Service("customerFacadeImpl")
public class CustomerFacadeImpl implements CustomerFacade {
	private Logger logger = Logger.getLogger(CustomerFacadeImpl.class);
	@Autowired
	private DutyseviceImp dutyseviceImp;
	
	@Autowired
	private CmsServerCtrlImpl cmsServerCtrlImpl;

	@Autowired
	private CommonCtrlImpl commonCtrlImpl ;
	
	
	/***
	 * 查询工资信息
	 */
	/* (non-Javadoc)
	 * @see com.twl.facade.web.CustomerFacade#findsalarydetail(java.lang.String)
	 */
	@Override
	public String findsalarydetail(String param) {
		// 将cms传进来的json转换成参数信息和Page
		Pagevo vo  =GsonUtils.toEntity(param, Pagevo.class);
		Pagevo result = dutyseviceImp.findsalarydetail(vo.getParam(),vo.getPage());
		return GsonUtils.toJson(result);
	}

	@Override
	public String getBfSalary(String param) {
		// 将cms传进来的json转换成参数信息和Page
		Map<String, Object> params = WsPageUtils.getParams(param);

		if (logger.isDebugEnabled()) {
			logger.debug("进入新进补发查询" + System.currentTimeMillis());
		}
		Result result = cmsServerCtrlImpl.getBfSalary(params);
		String resultStr = GsonUtils.toJson(result);

		return resultStr;
	}

	@Override
	public String getJsbfmoney(String param) {
		// 将cms传进来的json转换成参数信息和Page
		Map<String, Object> params = WsPageUtils.getParams(param);

		if (logger.isDebugEnabled()) {
			logger.debug("进入晋升补发查询" + System.currentTimeMillis());
		}
		Result result = cmsServerCtrlImpl.getJsbfmoney(params);
		String resultStr = GsonUtils.toJson(result);

		return resultStr;
	}

	@Override
	public String getMoneyadjust(String param) {
		// 将cms传进来的json转换成参数信息和Page
		Map<String, Object> params = WsPageUtils.getParams(param);

		if (logger.isDebugEnabled()) {
			logger.debug("进入正式或者派遣离职-扣减查询" + System.currentTimeMillis());
		}
		Result result = cmsServerCtrlImpl.getMoneyadjust(params);
		String resultStr = GsonUtils.toJson(result);

		return resultStr;
	}

	@Override
	public String getLeavebmoney(String param) {
		// 将cms传进来的json转换成参数信息和Page
		Map<String, Object> params = WsPageUtils.getParams(param);

		if (logger.isDebugEnabled()) {
			logger.debug("进入请假扣款查询" + System.currentTimeMillis());
		}
		Result result = cmsServerCtrlImpl.getLeavebmoney(params);
		String resultStr = GsonUtils.toJson(result);

		return resultStr;
	}

	@Override
	public String getOvertimemoney(String param) {
		// 将cms传进来的json转换成参数信息和Page
		Map<String, Object> params = WsPageUtils.getParams(param);

		if (logger.isDebugEnabled()) {
			logger.debug("进入加班工资查询" + System.currentTimeMillis());
		}
		Result result = cmsServerCtrlImpl.getOvertimemoney(params);
		String resultStr = GsonUtils.toJson(result);

		return resultStr;
	}

	@Override
	public String getOvertimemeals(String param) {
		// 将cms传进来的json转换成参数信息和Page
		Map<String, Object> params = WsPageUtils.getParams(param);

		if (logger.isDebugEnabled()) {
			logger.debug("进入加班餐补查询" + System.currentTimeMillis());
		}
		Result result = cmsServerCtrlImpl.getOvertimemeals(params);
		String resultStr = GsonUtils.toJson(result);

		return resultStr;
	}

	@Override
	public String getShiftallowance(String param) {
		// 将cms传进来的json转换成参数信息和Page
		Map<String, Object> params = WsPageUtils.getParams(param);

		if (logger.isDebugEnabled()) {
			logger.debug("进入轮班津贴查询" + System.currentTimeMillis());
		}
		Result result = cmsServerCtrlImpl.getShiftallowance(params);
		String resultStr = GsonUtils.toJson(result);

		return resultStr;
	}

	@Override
	public String getLoyalward(String param) {
		// 将cms传进来的json转换成参数信息和Page
		Map<String, Object> params = WsPageUtils.getParams(param);

		if (logger.isDebugEnabled()) {
			logger.debug("进入忠诚奖查询" + System.currentTimeMillis());
		}
		Result result = cmsServerCtrlImpl.getLoyalward(params);
		String resultStr = GsonUtils.toJson(result);

		return resultStr;
	}

	@Override
	public String getDutymeals(String param) {
		// 将cms传进来的json转换成参数信息和Page
		Map<String, Object> params = WsPageUtils.getParams(param);

		if (logger.isDebugEnabled()) {
			logger.debug("进入税前饭贴查询" + System.currentTimeMillis());
		}
		Result result = cmsServerCtrlImpl.getDutymeals(params);
		String resultStr = GsonUtils.toJson(result);

		return resultStr;
	}

	@Override
	public String getDispatchmanger(String param) {
		// 将cms传进来的json转换成参数信息和Page
		Map<String, Object> params = WsPageUtils.getParams(param);

		if (logger.isDebugEnabled()) {
			logger.debug("进入派遣员工管理费	查询" + System.currentTimeMillis());
		}
		Result result = cmsServerCtrlImpl.getDispatchmanger(params);
		String resultStr = GsonUtils.toJson(result);

		return resultStr;
	}

	@Override
	public String getSecureunit(String param) {
		// 将cms传进来的json转换成参数信息和Page
		Map<String, Object> params = WsPageUtils.getParams(param);

		if (logger.isDebugEnabled()) {
			logger.debug("进入保险单位金查询" + System.currentTimeMillis());
		}
		Result result = cmsServerCtrlImpl.getSecureunit(params);
		String resultStr = GsonUtils.toJson(result);

		return resultStr;
	}

	@Override
	public String getSolidmoney(String param) {
		// 将cms传进来的json转换成参数信息和Page
		Map<String, Object> params = WsPageUtils.getParams(param);

		if (logger.isDebugEnabled()) {
			logger.debug("进入实发工资查询" + System.currentTimeMillis());
		}
		Result result = cmsServerCtrlImpl.getSolidmoney(params);
		String resultStr = GsonUtils.toJson(result);

		return resultStr;
	}

	@Override
	public String getDutycount(String param) {
		// 将cms传进来的json转换成参数信息和Page
		Map<String, Object> params = WsPageUtils.getParams(param);

		if (logger.isDebugEnabled()) {
			logger.debug("进入税后应发合计查询" + System.currentTimeMillis());
		}
		Result result = cmsServerCtrlImpl.getDutycount(params);
		String resultStr = GsonUtils.toJson(result);

		return resultStr;
	}

	@Override
	public String getPerreportduty(String param) {
		// 将cms传进来的json转换成参数信息和Page
		Map<String, Object> params = WsPageUtils.getParams(param);

		if (logger.isDebugEnabled()) {
			logger.debug("进入个人所得税（报税）查询" + System.currentTimeMillis());
		}
		Result result = cmsServerCtrlImpl.getPerreportduty(params);
		String resultStr = GsonUtils.toJson(result);

		return resultStr;
	}

	@Override
	public String getPersonalduty(String param) {
		// 将cms传进来的json转换成参数信息和Page
		Map<String, Object> params = WsPageUtils.getParams(param);

		if (logger.isDebugEnabled()) {
			logger.debug("进入个人所得税查询" + System.currentTimeMillis());
		}
		Result result = cmsServerCtrlImpl.getPersonalduty(params);
		String resultStr = GsonUtils.toJson(result);

		return resultStr;
	}

	@Override
	public String getDutyback(String param) {
		Map<String, Object> params = WsPageUtils.getParams(param);

		if (logger.isDebugEnabled()) {
			logger.debug("进入税后饭贴查询" + System.currentTimeMillis());
		}
		Result result = cmsServerCtrlImpl.getDutyback(params);
		String resultStr = GsonUtils.toJson(result);

		return resultStr;
	}

	@Override
	public Moneydetail findmoneyate(String id, String date) {

		return dutyseviceImp.findmoneyate(id, date);
	}

	@Override
	public String getTradecost(String param) {
		// 将cms传进来的json转换成参数信息和Page
		Map<String, Object> params = WsPageUtils.getParams(param);

		if (logger.isDebugEnabled()) {
			logger.debug("进入工会费查询" + System.currentTimeMillis());
		}
		Result result = cmsServerCtrlImpl.getTradecost(params);
		String resultStr = GsonUtils.toJson(result);

		return resultStr;
	}

	@Override
	public String getDutysendmoney(String param) {
		// 将cms传进来的json转换成参数信息和Page
		Map<String, Object> params = WsPageUtils.getParams(param);

		if (logger.isDebugEnabled()) {
			logger.debug("进入税前应发工资查询" + System.currentTimeMillis());
		}
		Result result = cmsServerCtrlImpl.getDutysendmoney(params);
		String resultStr = GsonUtils.toJson(result);

		return resultStr;
	}

	@Override
	public String getDutydeduct(String param) {
		// 将cms传进来的json转换成参数信息和Page
		Map<String, Object> params = WsPageUtils.getParams(param);

		if (logger.isDebugEnabled()) {
			logger.debug("进入税前扣除查询" + System.currentTimeMillis());
		}
		Result result = cmsServerCtrlImpl.getDutydeduct(params);
		String resultStr = GsonUtils.toJson(result);

		return resultStr;
	}

	@Override
	public String getPersonalmoney(String param) {
		// 将cms传进来的json转换成参数信息和Page
		Map<String, Object> params = WsPageUtils.getParams(param);

		if (logger.isDebugEnabled()) {
			logger.debug("进入个人保险金查询" + System.currentTimeMillis());
		}
		Result result = cmsServerCtrlImpl.getPersonalmoney(params);
		String resultStr = GsonUtils.toJson(result);

		return resultStr;
	}

	@Override
	public String getDutymoney(String param) {
		// 将cms传进来的json转换成参数信息和Page
		Map<String, Object> params = WsPageUtils.getParams(param);

		if (logger.isDebugEnabled()) {
			logger.debug("进入税前工资查询" + System.currentTimeMillis());
		}
		Result result = cmsServerCtrlImpl.getDutymoney(params);
		String resultStr = GsonUtils.toJson(result);

		return resultStr;
	}

	@Override
	public String getJbsalary(String param) {
		// 将cms传进来的json转换成参数信息和Page
		Map<String, Object> params = WsPageUtils.getParams(param);

		if (logger.isDebugEnabled()) {
			logger.debug("进入税前工资查询" + System.currentTimeMillis());
		}
		Result result = cmsServerCtrlImpl.getJbsalary(params);
		String resultStr = GsonUtils.toJson(result);

		return resultStr;
	}

	@Override
	public String lockSalary(String date) {
		
		List<Uemp> uempList = commonCtrlImpl.getUempList();//查询所有未被删除的员工
		if(uempList!=null){
			//修改员工状态
			for (int i = 0; i < uempList.size(); i++) {//入职改在职
				if(uempList.get(i).getUmpstaff()!=null){
					if(0==uempList.get(i).getUmpstaff()){
						uempList.get(i).setUmpstaff(1);
						commonCtrlImpl.updateUemp(uempList.get(i));
					}else if(2==uempList.get(i).getUmpstaff()){//离职改逻辑删除
						uempList.get(i).setDelstaff(1);
						commonCtrlImpl.updateUemp(uempList.get(i));
					}
					if(1==uempList.get(i).getPromedeint()){//晋升状态修改
						uempList.get(i).setPromedeint(0);
						commonCtrlImpl.updateUemp(uempList.get(i));
					}
				}
			}
			/*for (Uemp uemp : uempList) {//入职改在职
				if(0==uemp.getUmpstaff()){
					uemp.setUmpstaff(1);
					commonCtrlImpl.updateUemp(uemp);
				}else if(2==uemp.getUmpstaff()){//离职改逻辑删除
					uemp.setDelstaff(1);
					commonCtrlImpl.updateUemp(uemp);
				}
			}*/
		}
		//查询表中是否存在这条记录，如果不存在则添加一条，并置lockCheck为“1” ；若存在则不添加
		boolean flag =commonCtrlImpl.queryLock(date);
		if(flag){
			return "THERE HAVE LOCK!";
		}else{
			return commonCtrlImpl.insertLock(date);
		}
	}

	
	//先向数据库查询 然后按传入的对象修改，若没查到就新增一条  open锁
	@Override
	public String lockOpen(OpenState openState) {
		if(openState.getOpenDate()==null || "".equals(openState.getOpenDate())){
			return "NO CHECKDATE!" ;
		}
		OpenState openStateNow = commonCtrlImpl.getOpenState(openState.getOpenDate());
		
		if(openStateNow!=null){
			openStateNow.setOpenCheck(openState.getOpenCheck());
			return commonCtrlImpl.saveOrUpdateOpenState(openStateNow);
		}else{
			
			return commonCtrlImpl.saveOrUpdateOpenState(openState);
			
		}
		
	}

}
